import 'package:flutter/material.dart';
import 'package:pui_flutter/src/constants/pui_constants.dart';
import 'package:pui_flutter/src/utils/pui_multi_click_util.dart';

/// 默认按钮字号
const double _defaultFontSize = 16;

/// 默认字体颜色
const Color _defaultTextColor = Colors.white;

/// 默认按钮背景色
const Color _defaultBackgroundColor = Color(0xFFFF682F);

/// 默认不可点击按钮背景色
const Color _defaultDisableBgColor = Color(0xFFCCCCCC);

class PPNormalButton extends StatelessWidget {
  /// 按钮是否可用
  final bool isEnabled;

  /// 按钮点击回调
  final VoidCallback? onTap;

  /// 按钮宽度
  final double? width;

  /// 按钮高度
  final double? height;

  /// 按钮显示的文案
  final String text;

  /// 按钮的文字颜色
  final Color textColor;

  /// 按钮的文字大小
  final double fontSize;

  /// 不可点击字体颜色
  final Color? disabledTextColor;

  /// 按钮背景色
  final Color color;

  /// 渐变背景色，如果同时设置了 bgColor 和 bgGradientColor， 则以渐变背景效果为主
  final List<Color>? gradientColor;

  /// 不可点击背景色
  final Color disabledColor;

  /// 不可点击透明色
  final double? disabledAlpha;

  /// 按钮内边距 默认水平[PUIButtonConstant.horizontalPadding]
  /// 垂直[PUIButtonConstant.verticalPadding]
  final EdgeInsetsGeometry padding;

  /// 按钮外边距
  final EdgeInsetsGeometry? margin;

  /// 按钮的文本Weight 默认是[FontWeight.normal]
  final FontWeight fontWeight;

  /// 按钮的文本显示样式 优先级高于[textColor]等其他属性
  final TextStyle? textStyle;

  /// 按钮的修饰 默认实色背景
  final Decoration? decoration;

  /// 按钮的显示子节点 优先级高于[text]
  final Widget? child;

  /// 按钮的布局约束 默认是自适应大小
  final BoxConstraints? constraints;

  /// 按钮的内部对齐 默认为null
  final Alignment? alignment;

  /// 按钮圆角大小
  final BorderRadius? borderRadius;

  /// 描边颜色
  final Color? borderColor;

  /// 描边大小
  final double borderWidth;

  const PPNormalButton({
    Key? key,
    required this.text,
    this.width,
    this.height,
    this.isEnabled = true,
    this.onTap,
    this.fontSize = _defaultFontSize,
    this.textColor = _defaultTextColor,
    this.disabledTextColor,
    this.color = _defaultBackgroundColor,
    this.gradientColor,
    this.disabledColor = _defaultDisableBgColor,
    this.disabledAlpha,
    this.fontWeight = FontWeight.normal,
    this.textStyle,
    this.child,
    this.constraints,
    this.alignment,
    this.decoration,
    this.borderRadius,
    this.borderWidth = 0,
    this.borderColor,
    this.padding = const EdgeInsets.symmetric(
        horizontal: PUIButtonConstant.horizontalPadding,
        vertical: PUIButtonConstant.verticalPadding),
    this.margin,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Widget widget = GestureDetector(
      onTap: _getGestureTapCallback(isEnabled),
      child: Container(
        decoration: decoration ??
            _getBoxDecoration(
                _getBackgroundColor(isEnabled, color, gradientColor,
                    disabledColor, disabledAlpha),
                borderColor,
                borderWidth),
        alignment: alignment ?? _getDefaultAligment(width),
        padding: padding,
        constraints: constraints ?? _getBoxConstraints(width, height),
        child: child ??
            Center(
              child: Text(
                text,
                overflow: TextOverflow.ellipsis,
                maxLines: 1,
                style: _getTextStyle(),
              ),
            ),
      ),
    );
    // 水波纹效果在导入 pp_flutter项目中出现水波纹异常的问题，暂时放弃水波纹效果
    // Widget widget = Ink(
    //   decoration: decoration ??
    //       _getBoxDecoration(_getBackgroundColor(
    //           isEnabled, color, gradientColor, disabledColor, disabledAlpha)),
    //   child: InkWell(
    //     onTap: _getGestureTapCallback(isEnabled),
    //     borderRadius: _getBorderRadius(),
    //     child: Container(
    //       alignment: alignment ?? _getDefaultAligment(width),
    //       padding: padding,
    //       constraints: constraints ?? _getBoxConstraints(width, height),
    //       child: child ??
    //           Text(
    //             text,
    //             overflow: TextOverflow.ellipsis,
    //             maxLines: 1,
    //             style: _getTextStyle(),
    //           ),
    //     ),
    //   ),
    // );
    return margin == null ? widget : wrapMarginContainer(widget, margin!);
  }

  /// 生成按钮点击回调对象onTap
  GestureTapCallback? _getGestureTapCallback(bool isEnabled) {
    if (!isEnabled || onTap == null) {
      return null;
    }
    return () {
      if (!PUIMultiClickUtils.isMultiClick()) {
        onTap!();
      }
    };
  }

  /// 生成按钮TextStyle对象
  TextStyle _getTextStyle() {
    if (textStyle != null) {
      return textStyle!;
    }
    Color textColor;
    if (isEnabled) {
      textColor = this.textColor;
    } else {
      textColor = disabledTextColor ?? (this.textColor).withOpacity(0.5);
    }
    return TextStyle(
      fontSize: fontSize,
      color: textColor,
      fontWeight: fontWeight,
    );
  }

  /// 生成背景色
  Gradient? _getBackgroundColor(
      bool isEnabled,
      Color bgColor,
      List<Color>? bgGradientColor,
      Color disableBgColor,
      double? disableBgAlpha) {
    // 如果按钮状态不可用，且disableBgAlpha无值，则直接返回disableBgColor对应的色值
    if (!isEnabled && disableBgAlpha == null) {
      return LinearGradient(colors: [disableBgColor, disableBgColor]);
    }
    // 获取渐变值
    List<Color>? colors = bgGradientColor;
    if (bgGradientColor != null && bgGradientColor.isNotEmpty) {
      // 如果颜色数组只有一个
      if (bgGradientColor.length == 1) {
        colors = [bgGradientColor[0], bgGradientColor[0]];
      }
    } else {
      colors = [bgColor, bgColor];
    }
    if (!isEnabled) {
      List<Color> disableColorList = [];
      for (int i = 0; i < colors!.length; i++) {
        disableColorList.add(colors[i].withOpacity(disableBgAlpha!));
      }
      colors = disableColorList;
    }
    return colors != null ? LinearGradient(colors: colors) : null;
  }

  /// 获取按钮修饰对象
  BoxDecoration _getBoxDecoration(
      Gradient? gradient, Color? borderColor, double borderWidth) {
    BoxBorder? border;
    if (borderWidth > 0 && borderColor != null) {
      BorderSide size = BorderSide(
          color: borderColor, width: borderWidth, style: BorderStyle.solid);
      border =
          BorderDirectional(start: size, top: size, end: size, bottom: size);
    }
    return BoxDecoration(
      gradient: gradient,
      border: border,
      borderRadius: _getBorderRadius(),
    );
  }

  BorderRadius _getBorderRadius() {
    return borderRadius ?? BorderRadius.circular(height ?? 100.0);
  }

  /// 按钮宽高限制对象
  BoxConstraints _getBoxConstraints(double? width, double? height) {
    return BoxConstraints.tightFor(width: width, height: height);
  }

  /// 获取子widget的对齐对象
  /// 在Container中，如果aligment值非空，则容器将展开撑到最大。
  /// 所以在设置默认aligment值时，需要根据控件传入的宽度来确定aligment是否有值
  /// 1.width有固定值且非0时：
  ///     aligment默认值为Alignment.center，此时由于设置了固定宽度，容器展开不影响控件宽度；
  /// 2.width无值时，此时容器应自适应宽度：
  ///     aligment默认值设置为null，避免容器展开撑到最大，影响本身自适应宽度的效果。
  Alignment? _getDefaultAligment(double? width) {
    if (width == null || width == 0) {
      return null;
    }
    return Alignment.center;
  }

  /// 通过Container包裹实现margin效果
  Widget wrapMarginContainer(Widget child, EdgeInsetsGeometry margin) {
    return Container(
      margin: margin,
      child: child,
    );
  }
}
